<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/1/08/0008
 * Time: 10:05
 */

namespace app\sports_admin\model;


use think\Model;

class OtherOperationLog extends Model
{
    protected $table = 'other_operation_log';

    protected $pk = 'id';

    protected $autoWriteTimestamp = true;

    public function getAccessTypeAttr($value)
    {
        $array = [1=>'手机小程序',2=>'APP',3=>'学校端',4=>'平板小程序'];
        return $array[$value];
    }

    public function getRequestIpAttr($value)
    {
        return long2ip($value);
    }
    /*
    * 获取访问数据
    * @param $page 第几页
    * @param $rows 每页显示的条数
    * @param $sort 要排序的字段
    * @param $sortOrder 升序/降序
    * @param $searchName 要检索的字段
    * @param $start 开始时间
    * @param $end 结束时间
    * @param $accessType 消息来源
    */
    public static function getInfoList($page,$rows,$sort,$sortOrder,$searchName,$start,$end,$accessType)
    {
        $result = self::where(function($query)use($searchName,$start,$end,$accessType){
            if($searchName){
                $query->where('username','like',"%{$searchName}%");
            }
            if($start && $end){
                $query->whereTime('create_time','between',["{$start}","{$end}"]);
            }
            if($accessType){
                $query->where('access_type',$accessType);
            }
        })->order("{$sort}","{$sortOrder}")->limit($rows)->page($page)->select();
        $count = self::where(function($query)use($searchName,$start,$end,$accessType){
            if($searchName){
                $query->where('username','like',"%{$searchName}%");
            }
            if($start && $end){
                $query->whereTime('create_time','between',["{$start}","{$end}"]);
            }
            if($accessType){
                $query->where('access_type',$accessType);
            }
        })->count();
        return json(['rows'=>$result,'total'=>$count]);
    }
}